/**
 * 
 */
package ae.pcfc.cldos.online.web.controller.client.company.ev.termination;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import ae.pcfc.cldos.online.common.ConstantsCommon.Steps;
import ae.pcfc.cldos.online.common.UltimusConstants.AdminDecision;
import ae.pcfc.cldos.online.common.delegate.EmploymentVisaCancelationDelegate;
import ae.pcfc.cldos.online.common.delegate.GeneralAdminDelegate;
import ae.pcfc.cldos.online.common.vo.BackOfficeAdminVO;
import ae.pcfc.cldos.online.common.vo.FileVO;
import ae.pcfc.cldos.online.common.vo.UserVO;
import ae.pcfc.cldos.online.common.vo.ev.ren.can.EmploymentVisaCancelationVO;
import ae.pcfc.cldos.online.web.Constants;
import ae.pcfc.cldos.online.web.PageType;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.ViewUtils;
import ae.pcfc.cldos.online.web.model.BackOfficeAdminReview;
import ae.pcfc.cldos.online.web.model.EVTerminationModel;

/**
 * 
 * @author arosa
 *
 */
@Controller
@RequestMapping(value = ViewPath.__ADMIN_EMPLOYMENT_VISA_TERMINATE_BACKOFFICE)
public class EVTerminationBackOfficeReviewController extends EVTerminationBase {
	
	@Autowired
	@Qualifier("employmentVisaCancelationDelegate")
	private EmploymentVisaCancelationDelegate employmentVisaCancelationDelegate;
	
	@Autowired
	@Qualifier("generalAdminDelegate")
	private GeneralAdminDelegate generalAdminDelegate;
	
	@ModelAttribute("evTerminationModel")
	public EVTerminationModel populateRegisterEmploymentVisaTerminationInfoModel(@RequestParam String appId,
			HttpServletRequest request, ModelMap model,
			@ModelAttribute(value = "backOfficeAdminReview") BackOfficeAdminReview adminReview) throws Exception {
		EVTerminationModel evTerminationModel = new EVTerminationModel();
		
		UserVO  userVO= getUserVO();
		userVO.setGeneralAdmin(true);
		
		EmploymentVisaCancelationVO employmentVisaCancelationVO = 
				employmentVisaCancelationDelegate.getEmploymentVisaCancelationAppInfo(
						appId,userVO, Steps.DOCUMENT_AND_DATA_VALIDATION);
		
		adminReview.setClientReferenceNo(employmentVisaCancelationVO.getClientReferenceNo());
		adminReview.setInitiatedBy(employmentVisaCancelationVO.getInitiatedBy());
		
		List<FileVO> fileVOList = employmentVisaCancelationVO.getFileVOList();
		model.put("files", fileVOList);
		model.put("comments", employmentVisaCancelationVO.getApplicationCommentVOs());
		
		mapper.map(employmentVisaCancelationVO.getEmploymentVisaCancelationDetailsVO(), evTerminationModel);
		
		return evTerminationModel;
	}
	
	
	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public void form(@RequestParam(value = "appId", required = true) String appid,
			@RequestParam(value = "UserName", required = true) String userName,
			@RequestParam(value = "ProcessName", required = false) String processName,
			@RequestParam(value = "TaskID", required = true) String taskID,
			@RequestParam(value = "Incident", required = true) String incident,
			@RequestParam(value = "StepName", required = true) String stepName,
			@ModelAttribute(value = "backOfficeAdminReview") BackOfficeAdminReview adminReview) {

		adminReview.setStrApplicationId(appid);
		adminReview.setStrUserName(userName);
		adminReview.setStrTaskId(taskID);
		adminReview.setStrIncidentNo(incident);
		adminReview.setStrStepName(stepName);
		adminReview.setProcessName(processName);
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/form.htm")
	public ModelAndView form(
			HttpServletRequest request,
			SessionStatus status,
			@ModelAttribute("evTerminationModel") EVTerminationModel evTerminationModel,
			BindingResult errors,
			@ModelAttribute(value = "backOfficeAdminReview") BackOfficeAdminReview adminReview
			) throws Exception {
		
		boolean isSubmitApproval = WebUtils.hasSubmitParameter(request, "MainUserApprove");
		boolean isSubmitReject = WebUtils.hasSubmitParameter(request, "MainUserReject");
		boolean isSubmitCancel = WebUtils.hasSubmitParameter(request, "MainUserCancel");
		
		BackOfficeAdminVO backOfficeAdminVO = new BackOfficeAdminVO();
		mapper.map(adminReview, backOfficeAdminVO);
		
		if(isSubmitApproval){
			System.out.println("BackOffice Approved...");
			backOfficeAdminVO.setStrActionType(AdminDecision.APPROVED.getAdminDecision());
		}else if(isSubmitReject){
			if(adminReview.getAdminComments()==null || adminReview.getAdminComments().isEmpty()){
				errors.reject("admin.comment.required");
			}
			System.out.println("BackOffice Rejected...");
			backOfficeAdminVO.setStrActionType(AdminDecision.REJECTED.getAdminDecision());
		} else if(isSubmitCancel){
			System.out.println("BackOffice Canceled...");
			backOfficeAdminVO.setStrActionType(AdminDecision.CANCELLED.getAdminDecision());
		}
		
		ModelAndView mav = null;
		
		if(!errors.hasErrors()){
			
			backOfficeAdminVO.setStrComments(adminReview.getAdminComments());
			String clientRefNo = generalAdminDelegate.reviewByBackOffice(backOfficeAdminVO);
			
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put(Constants.__REFRENCE_NO, clientRefNo);
	
			paramMap.put("Incident", adminReview.getStrIncidentNo());
			paramMap.put("StepName", adminReview.getStrStepName());
			paramMap.put("InitiatedBy", adminReview.getInitiatedBy());
	
			status.setComplete();
			mav =new ModelAndView(ViewUtils.getRedirect(ViewPath.__BACKOFFICE_CONFIRMATION, PageType.SHOW, paramMap));
		} else {
			mav = ViewUtils.DEFAULT_VIEW;
		}
		
		return mav;
	}

}
